package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import play.data.binding.NoBinding;
import play.data.validation.MaxSize;
import play.data.validation.Min;
import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
public class Intervention extends Model {
		
	@Required
	public String subjectIntervention;
	
	@Required
	@Lob
	@MaxSize(10000)
	public String descriptionIntervention;
	
	@Required
	@Temporal(TemporalType.DATE)
	public Date endDateIntervention;
	
	@Required
	@Temporal(TemporalType.DATE)
	public Date beginDateIntervention;
	
	@Required
	@Min(1)
	public int durationIntervention;
	
	@NoBinding
	public Date createdAt;
	
	@Transient
	public Statut statutIntervention;

	@Required
	@ManyToOne
	public Formation formationIntervention;

	@Required
	@ManyToOne
	public Enseignant enseignantIntervention;
	
	@Transient
	private double moyenneGenerale;
	
	@Transient
	private Map<String,Double> moyenneGeneraleAllCritere;
	 
	@OneToMany(mappedBy ="interventionEvaluation2", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
	public Set<Evaluation2> listEvaluation2Intervention;

	public Intervention(String subjectIntervention,
			String descriptionIntervention, Date endDateIntervention,
			Date beginDateIntervention, int durationIntervention,
			Formation formationIntervention, Enseignant enseignantIntervention) {
		super();
		this.subjectIntervention = subjectIntervention;
		this.descriptionIntervention = descriptionIntervention;
		this.endDateIntervention = endDateIntervention;
		this.beginDateIntervention = beginDateIntervention;
		this.durationIntervention = durationIntervention;
		this.formationIntervention = formationIntervention;
		this.enseignantIntervention = enseignantIntervention;
		this.listEvaluation2Intervention = new TreeSet<Evaluation2>();
		this.createdAt = new Date();
	}

	@Override
	public String toString() {
		return subjectIntervention;
	}

	public double getMoyenneIntervention() {
		return moyenneGenerale;
	}

	public void setMoyenneIntervention(double moyenneGenerale) {
		this.moyenneGenerale = moyenneGenerale;
	}

	public Map<String,Double> getMoyenneGeneraleAllCritere() {
		return moyenneGeneraleAllCritere;
	}

	public void setMoyenneGeneraleAllCritere(
			Map<String,Double> moyenneGeneraleAllCritere) {
		this.moyenneGeneraleAllCritere = moyenneGeneraleAllCritere;
	}

	public void getStatutIntervention() {
		Date today = new Date();
		if( this.beginDateIntervention.after(today) ){
			this.statutIntervention = Statut.SOON;
		}else{
			if (this.endDateIntervention.before(today))
				this.statutIntervention = Statut.DONE;
			else
				this.statutIntervention = Statut.PROGRESS;
		}
	}

}
